package com.mls.guardian.appst.chain.excutor;

import com.mls.guardian.appst.chain.TaskExecutor;
import com.mls.guardian.appst.chain.TaskInvoker;
import com.mls.guardian.appst.chain.TaskResult;
import com.mls.guardian.appst.shell.ShellScriptExecutorFuture;
import com.mls.guardian.common.FileUtil;
import com.mls.guardian.dataobj.ModuleMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by pengrenfang on 15/10/29.
 */
public class UnzipWarExcutor implements TaskExecutor {
    private static final Logger log = LoggerFactory.getLogger(UnzipWarExcutor.class);

    @Override
    public TaskResult exec(TaskInvoker invoker) {
        String sourceFolder = invoker.getConfig().getMoveDest() + File.separator + invoker.getProject().getProjectName();
        List<String> list = new ArrayList<String>();

        List<String> appList = invoker.getProject().getWebModuleList();
        List<String> appAliasList = invoker.getProject().getWebAliasList();

        if (invoker.getModuleList() != null && invoker.getModuleList().size() > 0) {
            //指定模块
            for (ModuleMessage item : invoker.getModuleList()) {
                int index = appList.indexOf(item.getModuleName());
                String filePath = sourceFolder + File.separator + item.getModuleName() + "-1.0.war";
                //最终目录使用别名
                list.add("unzip -o " + filePath + " -d " + sourceFolder + File.separator + appAliasList.get(index));
            }
        } else {
            //所有模块
            for (int i = 0; i < appList.size(); i++) {
                String moduleName = appList.get(i);
                String filePath = sourceFolder + File.separator + moduleName + "-1.0.war";
                //最终目录使用别名
                list.add("unzip -o " + filePath + " -d " + sourceFolder + File.separator + appAliasList.get(i));
            }
        }
        list.add("exit");

        TaskResult result = null;
        try {
            log.info("----开始解压文件----");
            //创建目录
            if (invoker.getModuleList() != null && invoker.getModuleList().size() > 0) {
                //指定模块
                for (ModuleMessage item : invoker.getModuleList()) {
                    int index = appList.indexOf(item.getModuleName());
                    String moduleName = appAliasList.get(index);

                    FileUtil.deleteFolder(Paths.get(sourceFolder, moduleName).toString());
                    if (Files.notExists(Paths.get(sourceFolder, moduleName))) {
                        Files.createDirectories(Paths.get(sourceFolder, moduleName));
                    }
                }
            } else {
                for (String item : appAliasList) {
                    FileUtil.deleteFolder(Paths.get(sourceFolder, item).toString());
                    if (Files.notExists(Paths.get(sourceFolder, item))) {
                        Files.createDirectories(Paths.get(sourceFolder, item));
                    }
                }
            }
            //执行命令解压
            result = new ShellScriptExecutorFuture(list).execute();
            log.info("----解压文件[{}]----", result.isSuc());

            //暂停一下
            int sleepSecond = invoker.getConfig().getRestartSleep();
            if (sleepSecond <= 0) {
                sleepSecond = 10;
            }
            try {
                Thread.sleep(sleepSecond * 1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (result.isSuc() == true) {
                return invoker.invoke();
            }
        } catch (Exception e) {
            log.info("----解压文件[{}]----", false);

            result = new TaskResult();
            result.setSuc(false);
            result.setError(e.getMessage());

            e.printStackTrace();
        } finally {
            log.info("----结束解压文件----");
        }

        return result;
    }
}
